package com.pinyougou.shop.controller;

import entity.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import util.FastDFSClient;

import javax.swing.plaf.multi.MultiMenuItemUI;

/**
 * 文件上传
 */
@RestController
public class UploadController {

//    文件服务器地址
    @Value("${FILE_SERVER_URL}")
    private String FILE_SERVER_URL;

    @RequestMapping(value = "/upload")
    public Result upload(MultipartFile file){
//        获取文件扩展名
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        try{
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/fdfs_client.conf");
//            执行上传
            String path = fastDFSClient.uploadFile(file.getBytes(), extName);
//            拼接返回的url和ip地址,拼装完整的url
            String url = FILE_SERVER_URL + path;
            return new Result(true,url);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"上传失败");
        }

    }
}
